package com.xgkj.diyiketang.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.xgkj.diyiketang.activity.MainActivity;


/**
 * 项目名称：ConsumSo
 * 类描述：Activity之间的跳转工具类
 * 创建人：资拥
 * 创建时间：16/5/11 上午10:07
 * 修改人：资拥
 * 修改时间：16/5/11 上午10:07
 * 修改备注：
 */
public class JumperUtils {

    public static void JumpTo(Activity activity, Class<?> cls) {
        try {
            Intent intent = new Intent(activity, cls);
            activity.startActivity(intent);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }


    public static void JumpTo(Activity activity, Class<?> cls, Bundle bundle) {
        try {
            Intent intent = new Intent(activity, cls);
            intent.putExtras(bundle);
            activity.startActivity(intent);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage() + "");
        }
    }


    /**
     * 一键返回首页的方法
     */
    public static void JumpToMainActivity(Activity activity) {
        try {
            Intent intent = new Intent(activity, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("go_home", "true");
            activity.startActivity(intent);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }

    /**
     * 一键返回首页任务Fragment
     */
    public static void JumpToMainActivityTask(Activity activity) {
        try {
            Intent intent = new Intent(activity, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("go_home", "true");
            activity.startActivity(intent);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }


    public static void JumpToForResult(Activity activity, Class<?> cls, int requestCode) {
        try {
            Intent intent = new Intent(activity, cls);
            activity.startActivityForResult(intent, requestCode);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }


    public static void JumpToForResult(Activity activity, Class<?> cls, int requestCode, Bundle bundle) {
        try {
            Intent intent = new Intent(activity, cls);
            intent.putExtras(bundle);
            activity.startActivityForResult(intent, requestCode);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }


    public static void JumpTo(Activity activity, String url) {
        try {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            activity.startActivity(intent);
        } catch (ActivityNotFoundException | NullPointerException e) {
            Log.e("JumperUtils", e.getMessage());
        }
    }
}
